package com.moody.hibernate.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.BankCustGuarantee;


/**
 	* A data access object (DAO) providing persistence and search support for BankCustGuarantee entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.moody.hibernate.domain.BankCustGuarantee
  * @author MyEclipse Persistence Tools 
 */

public class BankCustGuaranteeDAO extends HibernateDaoSupport  {
	     private static final Logger log = LoggerFactory.getLogger(BankCustGuaranteeDAO.class);
		//property constants
	public static final String CUST_ID = "custId";
	public static final String LOANER_NAME = "loanerName";
	public static final String GUARANTEE_COUNT = "guaranteeCount";
	public static final String BALANCE = "balance";
	public static final String COMPENSATORY_POSSIBILITY = "compensatoryPossibility";
	public static final String GUARANTOR_NAME = "guarantorName";
	public static final String GUARANTY_NAME = "guarantyName";
	public static final String GUARANTY_SUM = "guarantySum";
	public static final String GUARANTY_TERM = "guarantyTerm";
	public static final String GUARANTY_BALANCE = "guarantyBalance";
	public static final String GUARANTY_POSSIBILITY = "guarantyPossibility";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String UPDATE_USER_ID = "updateUserId";
	public static final String UPDATE_ORG_ID = "updateOrgId";
	public static final String UPDATE_DATE = "updateDate";



	protected void initDao() {
		//do nothing
	}
    
    public void save(BankCustGuarantee transientInstance) {
        log.debug("saving BankCustGuarantee instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
    public void update(BankCustGuarantee transientInstance) {
    	 log.debug("updating BankCustGuarantee instance");
         try {
             getHibernateTemplate().update(transientInstance);
             log.debug("update successful");
         } catch (RuntimeException re) {
             log.error("update failed", re);
             throw re;
         }
		
	}
    
	public void delete(BankCustGuarantee persistentInstance) {
        log.debug("deleting BankCustGuarantee instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public BankCustGuarantee findById( java.lang.String id) {
        log.debug("getting BankCustGuarantee instance with id: " + id);
        try {
            BankCustGuarantee instance = (BankCustGuarantee) getHibernateTemplate()
                    .get("com.moody.hibernate.domain.BankCustGuarantee", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(BankCustGuarantee instance) {
        log.debug("finding BankCustGuarantee instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding BankCustGuarantee instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from BankCustGuarantee as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByCustId(Object custId
	) {
		return findByProperty(CUST_ID, custId
		);
	}
	
	public List findByLoanerName(Object loanerName
	) {
		return findByProperty(LOANER_NAME, loanerName
		);
	}
	
	public List findByGuaranteeCount(Object guaranteeCount
	) {
		return findByProperty(GUARANTEE_COUNT, guaranteeCount
		);
	}
	
	public List findByBalance(Object balance
	) {
		return findByProperty(BALANCE, balance
		);
	}
	
	public List findByCompensatoryPossibility(Object compensatoryPossibility
	) {
		return findByProperty(COMPENSATORY_POSSIBILITY, compensatoryPossibility
		);
	}
	
	public List findByGuarantorName(Object guarantorName
	) {
		return findByProperty(GUARANTOR_NAME, guarantorName
		);
	}
	
	public List findByGuarantyName(Object guarantyName
	) {
		return findByProperty(GUARANTY_NAME, guarantyName
		);
	}
	
	public List findByGuarantySum(Object guarantySum
	) {
		return findByProperty(GUARANTY_SUM, guarantySum
		);
	}
	
	public List findByGuarantyTerm(Object guarantyTerm
	) {
		return findByProperty(GUARANTY_TERM, guarantyTerm
		);
	}
	
	public List findByGuarantyBalance(Object guarantyBalance
	) {
		return findByProperty(GUARANTY_BALANCE, guarantyBalance
		);
	}
	
	public List findByGuarantyPossibility(Object guarantyPossibility
	) {
		return findByProperty(GUARANTY_POSSIBILITY, guarantyPossibility
		);
	}
	
	public List findByInputUserId(Object inputUserId
	) {
		return findByProperty(INPUT_USER_ID, inputUserId
		);
	}
	
	public List findByInputOrgId(Object inputOrgId
	) {
		return findByProperty(INPUT_ORG_ID, inputOrgId
		);
	}
	
	public List findByInputDate(Object inputDate
	) {
		return findByProperty(INPUT_DATE, inputDate
		);
	}
	
	public List findByUpdateUserId(Object updateUserId
	) {
		return findByProperty(UPDATE_USER_ID, updateUserId
		);
	}
	
	public List findByUpdateOrgId(Object updateOrgId
	) {
		return findByProperty(UPDATE_ORG_ID, updateOrgId
		);
	}
	
	public List findByUpdateDate(Object updateDate
	) {
		return findByProperty(UPDATE_DATE, updateDate
		);
	}
	

	public List findAll() {
		log.debug("finding all BankCustGuarantee instances");
		try {
			String queryString = "from BankCustGuarantee";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public BankCustGuarantee merge(BankCustGuarantee detachedInstance) {
        log.debug("merging BankCustGuarantee instance");
        try {
            BankCustGuarantee result = (BankCustGuarantee) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(BankCustGuarantee instance) {
        log.debug("attaching dirty BankCustGuarantee instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(BankCustGuarantee instance) {
        log.debug("attaching clean BankCustGuarantee instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static BankCustGuaranteeDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (BankCustGuaranteeDAO) ctx.getBean("BankCustGuaranteeDAO");
	}

	public void deleteById(String serialNo) {
		
		String hql = "delete from BankCustGuarantee where serialNo = ?";
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		query.setParameter(0, serialNo);
		if(query.executeUpdate() > 0)
			log.info("删除成功");
		else
			log.info("删除0个元素");
		session.close();

	}

	
}